<template>
	<div style="display: inline-block;max-width: 450px">
		<el-date-picker
				v-model="form.dischargeDate"
				:picker-options="pickerOptions"
		></el-date-picker>
		<span v-if="message">{{ message }}</span>
	</div>
</template>
<script>
	import Vue from 'vue';
	import ElementUI from 'element-ui';
	import AdmissionModel from '../js/config/admission';
	import Service from 'winged/utils/wt-model-service';
	import FormUtils from 'winged/utils/wt-form-utils';
	import ModelUtils from 'winged/utils/wt-model-utils';
	Vue.use(ElementUI);
	var limit;
	export default {
		name: 'wt-case-discharge-date',
		props: {
			// 字段对象（包含字段配置信息）
			field: {
				type: Object,
				required: true,
			},
			// 表单数据对象
			form: {
				type: Object,
				required: true,
			},
			paramsData: {
				type: Object,
				default: null
			},
			
		},
		computed: {
			icon() {
				return this.loading ? 'loading' : 'date';
			}
		},
		data() {
			return {
				message: null,
				loading: false,
				date: this.form.dischargeDate,
				admissionDate:null,
				pickerOptions: {
					disabledDate(time) {
						if (time.getTime() < limit || time.getTime() > Date.now()){
								return true
						}
					}
				}
			}
		},
		watch:{
			'form'(val){
				this.load()
			}
		},
		methods:{
			load() {
				this.loading = true;
				let _this = this;
				Service.query(AdmissionModel, this.paramsData, function(data) {
					_this.loading = false;
					_this.setQueryResult(FormUtils.parseArray(data));
				}, function(error) {
					_this.loading = false;
					console.error(error);
					_this.message = '获取入院记录失败';
				});
			},
			setQueryResult(data) {
				if(data.length == 0){
					this.message = '暂时没有入院记录，请先填写入院记录';
				}else {
					ModelUtils.normalizeData(AdmissionModel, data[0]);
					limit = data[0].admissionDate;
					this.message = ''
				}
			}


		},
		created(){
			this.load();
		}
	}
</script>
